<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>点击</button>
    <script>
        //模拟数据
        let obj = {
            myname: "zhangsan",
            age: 20,
            height: "178cm"
        }

        /* 
            把对象转成 固定格式的字符串 
            ?myname=zhangsan&age=20 
        */


        // objToStr = (obj) => {
        //     let keys = Object.keys(obj);
        //     let values = Object.values(obj);
        //     console.log(keys);
        //     console.log(values);
        //     var arr = keys.map((key, index) => {
        //         console.log(key, index);
        //         return `${key}=${values[index]}`
        //     })
        //     return arr.join("&")
        // }
        // objToStr(obj)
        // document.querySelector("button").onclick = function () {
        //     //window.location.href 当前页面打开指定url页面,并向指定url页面提交数据
        //     window.location.href = `b.html?${objToStr(obj)}`
        // }


        //代码优化
        //对象数据转为固定格式的url字符串参数
        objToStr = obj => Object.keys(obj).map((key, index) => `${key}=${Object.values(obj)[index]}`).join("&");
        //点击跳转时携带参数发送给固定url
        document.querySelector("button").onclick = () => window.location.href = `b.html?${objToStr(obj)}`;
    </script>
</body>

</html>